package com.princess.demok.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.core.graphics.drawable.toBitmap
import com.princess.baselib.utils.LogUtils
import com.princess.demok.net.data.ImgPointBean
import com.github.chrisbanes.photoview.OnMatrixChangedListener
import com.github.chrisbanes.photoview.OnScaleChangedListener
import com.github.chrisbanes.photoview.PhotoView

/**
 * @author wq
 * @date 2020/8/4 下午4:57
 * @desc DragImageView
 */
class DragImageView : PhotoView {

    private var drawPoint = false

    private var originalBitmap: Bitmap? = null

    var mPointX = 0F

    var mPointY = 0F

    private var redis = 15f

    private var imgPointList: MutableList<ImgPointBean> = arrayListOf()

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
        context,
        attrs,
        defStyle
    )

    fun setScalePoint(drawPoint: Boolean, x: Float = 0f, y: Float = 0f) {

        this.drawPoint = drawPoint
        this.mPointX = x
        this.mPointY = y

        if (drawPoint) {
            if (originalBitmap == null) {
                originalBitmap = drawable?.toBitmap()
            }
            originalBitmap?.let {
                val tempBitmap = it.copy(Bitmap.Config.ARGB_8888, true)
                val canvas = Canvas(tempBitmap)
                val paint = Paint()
                paint.isAntiAlias = true
                paint.color = Color.parseColor("#009B65")
                canvas.drawCircle(x * it.width, y * it.height, 15f, paint)
                val srcRect = Rect(0, 0, it.width, it.height)
                val dst = Rect(0, 0, it.width, it.height)
                canvas.drawBitmap(tempBitmap, srcRect, dst, paint)

                val photoViewMatrix = Matrix()
                getSuppMatrix(photoViewMatrix)
                setImageBitmap(tempBitmap)
                setDisplayMatrix(photoViewMatrix);
            }
        } else {
            originalBitmap?.let {

                val photoViewMatrix = Matrix()
                getSuppMatrix(photoViewMatrix)
                setImageBitmap(it)
                setDisplayMatrix(photoViewMatrix);
            }
        }

    }

    fun setScalePoint(drawPoint: Boolean, imgPointList: MutableList<ImgPointBean>) {
        if (imgPointList.isNullOrEmpty()) {
            return
        }
        this.drawPoint = drawPoint

        this.imgPointList = imgPointList

        if (drawPoint) {
            if (originalBitmap == null) {
                originalBitmap = drawable?.toBitmap()
            }
            originalBitmap?.let {
                val tempBitmap = it.copy(Bitmap.Config.ARGB_8888, true)
                val canvas = Canvas(tempBitmap)
                imgPointList.forEach { imgPoint ->
                    val paint = Paint()
                    paint.isAntiAlias = true
                    paint.color = imgPoint.colorInt
                    canvas.drawCircle(
                        imgPoint.pointX * it.width,
                        imgPoint.pointY * it.height,
                        redis / scale,
                        paint
                    )
                    val srcRect = Rect(0, 0, it.width, it.height)
                    val dst = Rect(0, 0, it.width, it.height)
                    canvas.drawBitmap(tempBitmap, srcRect, dst, paint)
                }

                val photoViewMatrix = Matrix()
                getSuppMatrix(photoViewMatrix)
                setImageBitmap(tempBitmap)
                setDisplayMatrix(photoViewMatrix);
            }
        } else {
            originalBitmap?.let {

                val photoViewMatrix = Matrix()
                getSuppMatrix(photoViewMatrix)
                setImageBitmap(it)
                setDisplayMatrix(photoViewMatrix);
            }
        }

    }

    override fun setOnMatrixChangeListener(listener: OnMatrixChangedListener?) {
        super.setOnMatrixChangeListener(listener)
        LogUtils.i("回调setOnMatrixChangeListener")

    }


    override fun setOnScaleChangeListener(onScaleChangedListener: OnScaleChangedListener?) {
        super.setOnScaleChangeListener(onScaleChangedListener)
        LogUtils.i("setOnScaleChangeListener")
//        setScalePoint(true, imgPointList)
    }



}